package mamikeji.liaowa.ui.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import mamikeji.liaowa.R;
import mamikeji.liaowa.ui.activity.LoginActivity;
import mamikeji.liaowa.utils.ForContents;
import mamikeji.liaowa.utils.ForPreference;

/**
 * Created by 廉冲-android开发 on 2017/8/5.
 */
public class BaseFragment extends Fragment {
    private Dialog dialog;
    private AnimationDrawable animationDrawable;

    public void getBase(){
        //网络请求动画
        getDialog();
    }

    private void toggleDialog(boolean bool) {
        if (bool) {
            animationDrawable.start();
            dialog.show();
        } else {
            animationDrawable.stop();
            dialog.dismiss();
        }
    }

    //开
    protected void showLoading() {
        toggleDialog(true);
    }

    //关
    protected void hideLoading() {
        toggleDialog(false);
    }

    /**
     * 加载中的动画
     *
     * @return 将对话框对象直接返回
     */
    public void getDialog() {
        dialog = new Dialog(getActivity(), R.style.DialogLoading);
        dialog.setContentView(R.layout.activity_dialogloading);
        dialog.setCanceledOnTouchOutside(false);
        ImageView imageView_progress = (ImageView) dialog.findViewById(R.id.imageView_progress);
        imageView_progress.setBackgroundResource(R.drawable.loading_animlist);
        animationDrawable = (AnimationDrawable) imageView_progress.getBackground();
    }

    /**
     * 判断用户是否登录
     *
     * @return
     */
    public boolean getJudgeLogin() {
        if (ForPreference.getString(ForContents.USER_PHONE).equals("") ||
                ForPreference.getString(ForContents.USER_PHONE) == null) {

            startActivity(new Intent(getActivity(), LoginActivity.class));
            return false;

        } else {
            return true;
        }
    }
}
